package com.example.projectdemo.rvrv;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.projectdemo.R;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/***
 * 创建时间：2020/2/25 11:39
 * 创建人：10850
 * 功能描述：
 */
public class RightAdapter extends RecyclerView.Adapter<RightAdapter.RightHolder> {

    private Context mContext;
    private List<NavBean.DataBean> mList;

    private List<String> mStrings;

    public RightAdapter(Context context, List<NavBean.DataBean> list) {
        this.mList = list;
        this.mContext = context;

        mStrings = new ArrayList<>();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public RightHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.right_item, viewGroup, false);
        return new RightHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final RightHolder rightHolder, int i) {
        mStrings.clear();
        List<NavBean.DataBean.ArticlesBean> articles = mList.get(i).getArticles();
        for (int j = 0; j < articles.size(); j++) {
            mStrings.add(articles.get(j).getTitle());
        }

        rightHolder.mFlowLayout.setAdapter(new TagAdapter<String>(mStrings) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                View inflate = LayoutInflater.from(mContext).inflate(R.layout.right_item_tv, parent, false);
                TextView tv = (TextView) inflate;
                tv.setText(s);
                return tv;
            }
        });

        rightHolder.tvTitle.setText(mList.get(i).getName());

        if (mOnItemClickListener != null) {
            rightHolder.tvTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = rightHolder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(rightHolder.tvTitle, pos);
                }
            });

            rightHolder.tvTitle.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = rightHolder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(rightHolder.tvTitle, pos);
                    return false;
                }
            });
        }


//        rightHolder.mFlowLayout.setAdapter(new TagAdapter(mList) {
//            @Override
//            public View getView(FlowLayout parent, int position, Object o) {
//                View inflate = LayoutInflater.from(mContext).inflate(R.layout.right_item_tv, parent, false);
//
//
//                return null;
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class RightHolder extends RecyclerView.ViewHolder {

        private TagFlowLayout mFlowLayout;

        private TextView tvTitle;

        public RightHolder(@NonNull View itemView) {
            super(itemView);

            mFlowLayout = itemView.findViewById(R.id.fl);
            tvTitle = itemView.findViewById(R.id.righttv);
        }
    }
}
